﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Geodatabase;
using System.Collections.Generic;

namespace MyMap
{
    public partial class BarChart : Form
    {
        private IFeatureClass m_featureClass;
        public BarChart()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 设置要素类并初始化字段列表
        /// </summary>
        /// <param name="featureClass">要素类</param>
        public void SetFeatureClass(IFeatureClass featureClass)
        {
            m_featureClass = featureClass;
            RefreshFieldLists();
        }

        /// <summary>
        /// 刷新字段列表，过滤数值型字段
        /// </summary>
        private void RefreshFieldLists()
        {
            if (m_featureClass == null)
                return;

            try
            {
                // 清空现有列表
                listBoxUnselected.Items.Clear();
                listBoxSelected.Items.Clear();

                // 获取所有数值型字段
                List<string> numericFields = GetNumericFields(m_featureClass);

                // 添加到未选择列表
                foreach (string fieldName in numericFields)
                {
                    listBoxUnselected.Items.Add(fieldName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"刷新字段列表时出错: {ex.Message}", "错误",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// 获取要素类中的所有数值型字段
        /// </summary>
        /// <param name="featureClass">要素类</param>
        /// <returns>数值型字段名称列表</returns>
        private List<string> GetNumericFields(IFeatureClass featureClass)
        {
            List<string> numericFields = new List<string>();

            if (featureClass == null)
                return numericFields;

            try
            {
                IFields fields = featureClass.Fields;

                for (int i = 0; i < fields.FieldCount; i++)
                {
                    IField field = fields.Field[i];

                    // 检查是否为数值型字段
                    if (IsNumericField(field))
                    {
                        numericFields.Add(field.Name);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"获取数值型字段时出错: {ex.Message}", "错误",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return numericFields;
        }

        /// <summary>
        /// 判断字段是否为数值型字段
        /// </summary>
        /// <param name="field">字段对象</param>
        /// <returns>如果是数值型字段返回true，否则返回false</returns>
        private bool IsNumericField(IField field)
        {
            if (field == null)
                return false;

            // 检查字段类型是否为数值型
            switch (field.Type)
            {
                case esriFieldType.esriFieldTypeSmallInteger:
                case esriFieldType.esriFieldTypeInteger:
                case esriFieldType.esriFieldTypeSingle:
                case esriFieldType.esriFieldTypeDouble:
                    return true;
                case esriFieldType.esriFieldTypeOID:
                    // 通常OID字段也是整数，但可能不需要参与统计，根据需求决定是否包含
                    // return true; // 如果需要包含OID字段，取消注释此行
                    return false; // 通常不包含OID字段
                default:
                    return false;
            }
        }

        /// <summary>
        /// 获取已选择的字段列表
        /// </summary>
        /// <returns>已选择的字段名称列表</returns>
        public List<string> GetSelectedFields()
        {
            List<string> selectedFields = new List<string>();

            foreach (var item in listBoxSelected.Items)
            {
                selectedFields.Add(item.ToString());
            }

            return selectedFields;
        }

        private void ButtonSelect_Click(object sender, EventArgs e)
        {
            if (listBoxUnselected.SelectedItem != null)
            {
                string selectedField = listBoxUnselected.SelectedItem.ToString();
                listBoxSelected.Items.Add(selectedField);
                listBoxUnselected.Items.Remove(selectedField);
            }
        }

        private void ButtonUnSelect_Click(object sender, EventArgs e)
        {
            if (listBoxSelected.SelectedItem != null)
            {
                string selectedField = listBoxSelected.SelectedItem.ToString();
                listBoxUnselected.Items.Add(selectedField);
                listBoxSelected.Items.Remove(selectedField);
            }
        }

        private void ButtonOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void ButtonCANCEL_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
       

        private void ListBoxUnselected_DoubleClick_1(object sender, EventArgs e)
        {
            ButtonSelect_Click(sender, e);
        }

        private void ListBoxSelected_DoubleClick_1(object sender, EventArgs e)
        {
            ButtonUnSelect_Click(sender, e);
        }
        public int GetChartWidth()
        {
           
          
                return (int)numericUpDownWidth.Value;
           
        }

        /// <summary>
        /// 获取用户设置的图表高度
        /// </summary>
        /// <returns>图表高度</returns>
        public int GetChartHeight()
        {
           
                return (int)numericUpDownHeight.Value;
           
        }
    }
}
